package com.tamecode;

import com.tamecode.service.ProcessService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

/**
 * 启动时会加载 classpath 下的所有的 BMPB2.0
 *
 * @Author LiQc
 */
@SpringBootApplication
public class FlowableApplication {

    public static void main(String[] args) {
        SpringApplication.run(FlowableApplication.class, args);
    }

    /**
     * Number of process definitions : 22
     * Number of tasks : 17
     * Number of tasks after process start: 18
     *
     * @param repositoryService
     * @param runtimeService
     * @param taskService
     * @return
     */
//    @Bean
    public CommandLineRunner init(final RepositoryService repositoryService,
                                  final RuntimeService runtimeService,
                                  final TaskService taskService) {

        return (String... strings) -> {
                System.out.println("Number of process definitions : "
                        + repositoryService.createProcessDefinitionQuery().count());
                System.out.println("Number of tasks : " + taskService.createTaskQuery().count());
                runtimeService.startProcessInstanceByKey("oneTaskProcess");
                System.out.println("Number of tasks after process start: "
                        + taskService.createTaskQuery().count());
            };
    }

    /**
     * 启动进创建用户
     * @param myService
     * @return
     */
    @Bean
    public CommandLineRunner init(final ProcessService myService) {

        return new CommandLineRunner() {
            @Override
            public void run(String... strings) throws Exception {
                myService.createDemoUsers();
            }
        };
    }


}
